<template>
    <div>
        <basic-container>
            <avue-crud
                    :data="list"
                    :option="tableOption"
                    ref="crud"
                    v-model="form"
                    @on-load="getList"
                    :table-loading="listLoading"
                    :page="page"
                    :permission="permission"
                    @search-change="handleFilter"
                    @refresh-change="handleRefreshChange"
                    @row-save="save"
                    @row-update="update"
                    @row-del="deletes"
            >
                <template slot="menu"
                          slot-scope="scope">
                    <el-button size="mini"
                               v-if="scope.row.state==='0'"
                               type="text"
                               icon="el-icon-plus"
                               plain
                               @click="handleSubmit(scope.row)">提交
                    </el-button>
                </template>
            </avue-crud>
            <el-dialog title="选择审批人员"
                       :visible.sync="dialogVisible">
                <el-select v-model="selectUser" multiple placeholder="请选择">
                    <el-option
                            v-for="item in personOptions"
                            :key="item.userId"
                            :label="item.userName"
                            :value="item.userId">
                    </el-option>
                </el-select>
                <div slot="footer"
                     class="dialog-footer">
                    <el-button type="primary"
                               @click="submit(selectUser)">提交
                    </el-button>
                </div>
            </el-dialog>
        </basic-container>
    </div>
</template>

<script>
    import {listPage,save,update,deleteLeave,submit,userList} from "@/api/activiti/leave";
    import {tableOption} from '@/const/activiti/leave';
    import {mapGetters} from "vuex";

    export default {
        data() {      return {
            dialogVisible: false,
            actPicUrl: "",
            list: [],
            page: {
                total: 0, // 总页数
                currentPage: 1, // 当前页数
                pageSize: 20 // 每页显示多少条
            },
            listLoading: false,
            form: {},
            hasSubmitPermission: false,
            permission:{},
            tableOption: tableOption,
            selectUser: '',
            personOptions:[],
        };
        },
        computed: {
            ...mapGetters(["permissions"])
        },
        created() {
            this.permission = {
                viewBtn: this.permissions['sys_activiti_leave_view']?true:false,
                addBtn: this.permissions['sys_activiti_leave_add']?true:false,
                delBtn: this.permissions['sys_activiti_leave_del']?true:false,
                editBtn: this.permissions['sys_activiti_leave_edit']?true:false,
                searchBtn: this.permissions['sys_activiti_leave_view']?true:false,
            }
        },
        methods: {
            //获取分页数据
            getList(page, params) {
                this.listLoading = true;
                listPage(Object.assign({
                    current: page.currentPage,
                    size: page.pageSize
                }, params)).then(response => {
                    this.list = response.data.data.records
                    this.page.total = response.data.data.total
                    this.listLoading = false;
                })
            },
            save(row, done,loading) {
                save(this.form).then(() => {
                    this.getList(this.page);
                    done();
                    this.$notify({
                        title: "成功",
                        message: "创建成功",
                        type: "success",
                        duration: 2000
                    });
                }).catch(() => {
                    loading();
                });
            },
            handleSubmit(row){
                this.form =row;
                userList().then((response) =>{
                    this.personOptions = response.data.data
                    this.getList(this.page);
                });
                this.dialogVisible = true
            },
            update(row, done,loading) {
                update(this.form).then(() => {
                    this.getList(this.page);
                    done();
                    this.$notify({
                        title: "成功",
                        message: "更新成功",
                        type: "success",
                        duration: 2000
                    });
                }).catch(() => {
                    loading();
                });
            },
            deletes(row,index){
                this.$confirm(
                    "此操作将永久删除, 是否继续?",
                    "提示",
                    {
                        confirmButtonText: "确定",
                        cancelButtonText: "取消",
                        type: "warning"
                    }
                ).then(() =>{
                    deleteLeave(row.id).then(() =>{
                        this.list.splice(index, 1);
                        this.$notify({
                            title: "成功",
                            message: "删除成功",
                            type: "success",
                            duration: 2000
                        });
                        this.getList(this.page);
                    });
                })
            },
           submit(selectUser){
                submit({'userIds':selectUser,'processId':this.form.id}).then(() =>{
                    this.getList(this.page);
                    this.dialogVisible = false
                });
            },
            handleFilter(param) {
                this.page.page = 1;
                this.getList(this.page, param);
            },
            handleRefreshChange() {
                this.getList(this.page)
            }
        }
    }
</script>
